
package combat.dudes.client;

import java.awt.Point;

import combat.dudes.DudesConfig;
import combat.dudes.client.gui.threads.map.ClientGUIMap.MapType;
import combat.dudes.client.gui.threads.map.main.GUIMainMap;
import combat.dudes.client.gui.threads.map.mini.GUIMiniMap;
import combat.dudes.protocol.data.DoublePoint;
import combat.dudes.protocol.interfaces.OrderInt;


/**
 * Functions that are very GUI-specific
 * @author Tim Claassens
 * @author Laurens Andriessen
 */
public class ClientFunctions {

	public static class GUI {

		public static String getTarget() {
			String target = "No targets set";
			if(ClientApp.SharedVars.getMyVehicle() != null) {
				OrderInt LastOrder = null;
				if(ClientApp.SharedVars.getMyVehicle().getOrders().size() > 0)
					LastOrder = ClientApp.SharedVars.getMyVehicle().getOrders().get(ClientApp.SharedVars.getMyVehicle().getOrders().size() - 1);
				else target = "No orders found";

				if(LastOrder != null) target = LastOrder.getTarget().toString();
			}

			return target;
		}

		/**
		 * Converts a DoublePoint to a point within the map in the GUI.
		 * @param type MapType.MAIN (main map) or MapType.MINI (mini map)
		 * @param point Coordinate from the server
		 * @return
		 */
		public static DoublePoint convertPointToMap(MapType type, DoublePoint point) {
			int w = 0, h = 0;
			switch(type) {
				case MAIN:
					w = GUIMainMap.mapWidth;
					h = GUIMainMap.mapHeight;
					break;
				case MINI:
					w = GUIMiniMap.mapWidth;
					h = GUIMiniMap.mapHeight;
					break;
			}
			return new DoublePoint((w / DudesConfig.SharedVars.getMapSize().getX() * point.getX()),
									(h - (h / DudesConfig.SharedVars.getMapSize().getY() * point.getY())));
		}

		/**
		 * Converts a click on the map in the GUI to a point within the map.
		 * @param type MapType.MAIN (main map) or MapType.MINI (mini map)
		 * @param point Position where the user clicked within the window
		 * @return
		 */
		public static DoublePoint convertMapToPoint(MapType type, Point point) {
			int w = 0, h = 0;
			switch(type) {
				case MAIN:
					w = GUIMainMap.mapWidth;
					h = GUIMainMap.mapHeight;
					break;
				case MINI:
					w = GUIMiniMap.mapWidth;
					h = GUIMiniMap.mapHeight;
					break;
			}
			return new DoublePoint((point.getX() / w * DudesConfig.SharedVars.getMapSize().getX()),
					(DudesConfig.SharedVars.getMapSize().getY() - (point.getY() / h * DudesConfig.SharedVars.getMapSize().getY())));
		}
	}
}
